package cn.wenhe9.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.wenhe9.common.utils.ResultResponse;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.wenhe9.gulimall.product.entity.CategoryEntity;
import cn.wenhe9.gulimall.product.service.CategoryService;

import javax.annotation.Resource;


/**
 * 商品三级分类
 *
 * @author dujinliang
 * @email dujinliang9@163.com
 * @date 2022-04-14 19:17:07
 */
@Api(tags = "产品分类管理接口")
@RestController
@RequestMapping("/api/product/category")
public class CategoryController {
    @Resource
    private CategoryService categoryService;


    /**
     * 查询所有分类以及子分类，以树形结构组装起来
     * @return ResultResponse
     */
    @ApiOperation("查询所有分类以及子分类，以树形结构组装起来")
    @GetMapping("/list/tree")
    public ResultResponse list(){
        List<CategoryEntity> categoryList = categoryService.listWithTree();

        return ResultResponse.ok(categoryList);
    }


    /**
     * 批量删除目录
     * @param catIds Long[]
     * @return ResultResponse
     */
    @ApiOperation("批量删除目录")
    @DeleteMapping("/delete")
    public ResultResponse removeBatch(@RequestBody Long[] catIds){

        categoryService.removeMenuByIds(Arrays.asList(catIds));

        return ResultResponse.ok();
    }

    @ApiOperation("根据id查询目录信息")
    @GetMapping("/info/{catId}")
    public ResultResponse getInfoById(@PathVariable(name = "catId") Long catId){
        CategoryEntity category = categoryService.getById(catId);
        return ResultResponse.ok(category);
    }

    @ApiOperation("保存分类目录信息")
    @PostMapping("/save")
    public ResultResponse saveNewCategory(@RequestBody CategoryEntity category){
        categoryService.save(category);
        return ResultResponse.ok();
    }

    /**
     * 更新分类目录信息
     * @param category PmsCategory
     * @return ResultResponse
     */
    @ApiOperation("更新分类目录信息")
    @PutMapping("/update")
    public ResultResponse updateCateory(@RequestBody CategoryEntity category){
        categoryService.updateDetail(category);
        return ResultResponse.ok();
    }

    /**
     * 更新分类目录的排序信息
     * @param categoryList List<PmsCategory>
     * @return ResultResponse
     */
    @ApiOperation("更新分类目录的排序信息")
    @PutMapping("/update/sort")
    public ResultResponse updateCateorySort(@RequestBody List<CategoryEntity> categoryList){
        categoryService.updateBatchById(categoryList);
        return ResultResponse.ok();
    }
}
